package com.jjc.android.util

import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.base.BaseActivity
import com.jjc.android.base.BaseFragment
import com.jjc.android.view.getQuickAdapter


abstract class SimpleOnRefreshLoadMoreListener(private val forActivity:Boolean = true) : OnRefreshLoadMoreListener {
    private var smartRefreshLayout:SmartRefreshLayout? = null
    private var recyclerView: RecyclerView? = null
    open fun invokeActivity(block: BaseActivity.() -> Unit){

    }

    open fun invokeFragment(block: BaseFragment.() -> Unit){

    }

    private var pageIndex = Constants.START_PAGE_NO
    private var useRecyclerViewContainer = true

    private fun showLoadingView(){
        if(forActivity){
            invokeActivity {
                if(useRecyclerViewContainer){
                    recyclerView?.let {
                        showLoadingView(it)
                    }
                }else{
                    smartRefreshLayout?.let {
                        showLoadingView(it)
                    }
                }
            }
        }else{
            invokeFragment {
                if(useRecyclerViewContainer){
                    recyclerView?.let {
                        showLoadingView(it)
                    }
                }else{
                    smartRefreshLayout?.let {
                        showLoadingView(it)
                    }
                }
            }
        }
    }

    fun init(sRefreshLayout: SmartRefreshLayout? = null,rView: RecyclerView? = null,useRecyclerViewContainer:Boolean = true,autoRefresh:Boolean = true){
        this.useRecyclerViewContainer = useRecyclerViewContainer
        if(forActivity){
            invokeActivity {
                smartRefreshLayout = sRefreshLayout ?: findViewById<SmartRefreshLayout?>(R.id.smartRefreshLayout)
                recyclerView = rView?:findViewById<RecyclerView?>(R.id.recyclerView)
                smartRefreshLayout?.setOnRefreshLoadMoreListener(this@SimpleOnRefreshLoadMoreListener)
            }
        }else{
            invokeFragment {
                smartRefreshLayout = sRefreshLayout
                recyclerView = rView
                smartRefreshLayout?.setOnRefreshLoadMoreListener(this@SimpleOnRefreshLoadMoreListener)
            }
        }
        if(autoRefresh){
            requestRefresh()
        }
    }

    private val actionSuccess:(Int,List<Any>?)->Unit = {page,data->
        if(page== Constants.START_PAGE_NO){
            smartRefreshLayout?.finishRefresh()
            if(data.isNullOrEmpty()){
                if(forActivity){
                    invokeActivity{
                        showEmptyView()
                    }
                }else{
                    invokeFragment{
                        showEmptyView()
                    }
                }
            }else{
                recyclerView?.getQuickAdapter<Any>()?.setNewData(data)
                if(forActivity){
                    invokeActivity {
                        hideLoadingView()
                    }
                }else{
                    invokeFragment {
                        hideLoadingView()
                    }
                }

            }
        }else{
            if(!data.isNullOrEmpty()){
                recyclerView?.getQuickAdapter<Any>()?.addData(data)
            }
            smartRefreshLayout?.finishLoadMore()
        }
        smartRefreshLayout?.let {
            if(it.refreshFooter==null){
                it.setRefreshFooter(ClassicsFooter(it.context))
            }
        }

        smartRefreshLayout?.setNoMoreData(data.isNullOrEmpty()||data.size<Constants.PAGE_SIZE)
        pageIndex = page
    }

    private val actionError:(Int, String?)->Unit = { page, error->
        if(page== Constants.START_PAGE_NO){
            smartRefreshLayout?.finishRefresh()
            if(forActivity){
                invokeActivity{
                    showErrorView(error)
                }
            }else{
                invokeFragment{
                    showErrorView(error)
                }
            }

        }else{
            smartRefreshLayout?.finishLoadMoreWithNoMoreData()
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        pageIndex = Constants.START_PAGE_NO
        loadPage(pageIndex,actionSuccess,actionError)
    }

    fun requestRefresh(isShowLoading:Boolean = true){
        if(isShowLoading){
            showLoadingView()
        }
        pageIndex = Constants.START_PAGE_NO
        loadPage(pageIndex,actionSuccess,actionError)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        loadPage(pageIndex+1,actionSuccess,actionError)
    }

    abstract fun loadPage(page:Int,actionSuccess:(Int,List<Any>?)->Unit,actionError:(Int,String?)->Unit)


}